<template>
  <div class="app-container">
    <el-card>
      <div class="btn_wrapper">
        <span>说明：目前支持学科和关键字条件筛选</span>
        <el-button type="success" icon="el-icon-edit" size="small" @click="jumpAddOrEdit('add')">新增试题</el-button>
      </div>

      <!-- 搜索表单 -->
      <el-form ref="form" :model="searchForm" label-width="80px">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="学科">
              <el-select v-model="searchForm.subjectID" placeholder="请选择"  @change="getCatalogAndTagsList">
                <el-option v-for="item in subjects" :key="item.id" :label="item.subjectName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select v-model="searchForm.catalogID" placeholder="请选择">
                <el-option v-for="item in catalogs" :key="item.id" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签">
              <el-select v-model="searchForm.tags" placeholder="请选择">
                <el-option v-for="item in tagsList" :key="item.value" :label="item.label" :value="item.label"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input v-model="searchForm.keyword"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select v-model="searchForm.questionType" placeholder="请选择">
                <el-option v-for="item in questionType" :key="item.value" :label="item.label" :value="item.label"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select v-model="searchForm.difficulty" placeholder="请选择">
                <el-option v-for="item in difficulty" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select v-model="searchForm.direction" placeholder="请选择">
                <el-option v-for="(item, index) in direction" :key="index" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select v-model="searchForm.ceartorID" placeholder="请选择">
                <el-option v-for="(item, index) in users" :key="index" :label="item.username" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="题目备注">
              <el-input v-model="searchForm.remark"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="searchForm.shortName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
              <el-select v-model="searchForm.province" style="width: 96px; margin-right:10px ;" placeholder="请选择" @change="choseProvince">
                <el-option v-for="(item, index) in provinceList" :key="index" :label="item" :value="item"></el-option>
              </el-select>
              <el-select v-model="searchForm.city" style="width: 96px;" placeholder="请选择">
                <el-option v-for="(item, index) in cityList" :key="index" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="text-align: right;">
              <el-button size="small" @click="clearFrom">清除</el-button>
              <el-button type="primary" size="small" @click="searchQuestion">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 提示 -->
      <el-alert title="" type="info" show-icon :closable="false" style="margin-bottom:20px;">
        <span class="el-alert-title">数据一共 {{ total }} 条</span>
      </el-alert>
      <!-- 表格 -->
      <el-table :data="questionsList" style="width: 100%;margin-bottom: 20px;">
        <el-table-column prop="number" label="试题编号" width="120"> </el-table-column>
        <el-table-column prop="subject" label="学科" width="80"> </el-table-column>
        <el-table-column prop="catalog" label="目录" width="80"> </el-table-column>
        <el-table-column prop="questionType" label="题型" width="80">
          <template slot-scope="scope">
            {{ scope.row.questionType | questionTypeFilter }}
          </template>
        </el-table-column>
        <el-table-column prop="question" label="题干" width="280"> </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="160"> </el-table-column>
        <el-table-column label="难度" width="80">
          <template slot-scope="scope">
            {{ scope.row.difficulty | difficultyFilter }}
          </template>
        </el-table-column>
        <el-table-column prop="creator" label="录入人" width="100"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="small" title="预览" type="primary" icon="el-icon-view" plain circle @click="handleDetail(scope.row)"></el-button>
            <el-button size="small" title="修改" type="success" icon="el-icon-edit" plain circle @click="jumpAddOrEdit('edit', scope.row)"></el-button>
            <el-button size="small" title="删除" type="danger" icon="el-icon-delete" plain circle @click="delQuesqion(scope.row)"></el-button>
            <el-button size="small" title="加入精选" type="warning" icon="el-icon-check" plain circle @click="addChoiceness(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 预览弹框 -->
      <!-- <el-dialog title="题目预览" :visible.sync="dialogVisible" width="60%">
        <el-row style="margin-bottom:20px">
          <el-col :span="6"> 【题型】: {{ questionsDetail.questionType | questionTypeFilter }} </el-col>
          <el-col :span="6"> 【编号】: {{ questionsDetail.id }} </el-col>
          <el-col :span="6"> 【难度】: {{ questionsDetail.difficulty | difficultyFilter }} </el-col>
          <el-col :span="6"> 【标签】: {{ questionsDetail.tags }} </el-col>
        </el-row>
        <el-row>
          <el-col :span="6"> 【学科】: {{ questionsDetail.subjectID }} </el-col>
          <el-col :span="6"> 【目录】: {{ questionsDetail.catalogID }} </el-col>
          <el-col :span="6"> 【方向】: {{ questionsDetail.direction }} </el-col>
        </el-row>
        <hr />
        <el-row>
          <el-col :span="24">
            【题干】:
            <div style="margin:15px 0;color:blue;">{{ questionsDetail.question }}</div>
          </el-col>
        </el-row>

        <div style="margin-bottom:15px">{{ questionsDetail.questionType | questionTypeFilter }} 选项: (以下选中的选项为正确答案)</div>
        <el-radio-group v-if="questionsDetail.questionType==='1'" v-model="radio">
          <el-radio  style="margin-bottom:10px" v-for="item in questionsDetail.options" :key="item.id" :label="item.isRight">{{ item.title }}</el-radio>
        </el-radio-group>

        <el-checkbox-group v-else  v-model="checkList">
          <el-checkbox style="margin-bottom:10px"  v-for="item in questionsDetail.options"  :key="item.id" :label="item.isRight">{{ item.title }}</el-checkbox>
        </el-checkbox-group>

        <hr />
        <div>【参考答案】 : <el-button type="danger" size="small">视频答案预览</el-button></div>
        <hr />
        <div style="margin:15px 0">【答案解析】 : {{ questionsDetail.answer }}</div>
        <hr />
        <div style="margin-top:15px">【题目备注】 : {{ questionsDetail.remarks }}</div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" type="primary">关闭</el-button>
        </span>
      </el-dialog> -->

      <QuestionPreview :questionsDetail="questionsDetail" :isShow="dialogVisible" @close="changedialogVisible"></QuestionPreview>
      <!-- 分页 -->
      <el-pagination
        style="text-align: right;"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pagesize"
        layout=" prev, pager, next,sizes,jumper"
        :total="total"
      >
        >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { provinces, citys } from '@/api/hmmm/citys' // 省市列表
import { list, remove, detail, choiceAdd } from '@/api/hmmm/questions' // 题目列表,删除,详情,加入精选
import { list as subjectList } from '@/api/hmmm/subjects' // 学科列表
import { simple } from '@/api/hmmm/directorys' // 目录列表
import { simple as tagsList } from '@/api/hmmm/tags' // 标签列表
import { list as companysList } from '@/api/hmmm/companys' // 公司列表
import { simple as usersList } from '@/api/base/users' // 用户列表
import { parseTime, html2Text } from '@/utils/index'
import { difficulty, questionType, direction } from '@/api/hmmm/constants' // 难度、题型、方向
import QuestionPreview from '../components/questions-preview'
export default {
  name: 'QuestionsList',
  components: { QuestionPreview },
  data () {
    return {
      page: 1,
      pagesize: 5,
      total: null,
      dialogVisible: false,
      questionsDetail: {}, // 基础题库详情
      searchForm: {
        subjectID: '', // 学科
        difficulty: '', // 难度
        questionType: '', // 试题类型
        tags: '', // 标签名称
        keyword: '', // 关键字
        direction: '', // 方向
        remark: '', // 题目备注
        shortName: '', // 企业简称
        city: '', // 城市
        province: '', // 省份
        ceartorID: '', // 录入人
        catalogID: '' // 目录
      },
      questionsList: [], // 基础题库列表
      type: '',
      subjects: [], // 学科列表
      catalogs: [], // 目录列表
      tagsList: [], // 标签列表
      companys: [], // 企业列表
      cityList: [], // 城市列表
      provinceList: provinces(), // 省份列表
      difficulty,
      questionType,
      direction,
      users: [] // 录入人列表

    }
  },
  created () {
    this.getQuestionsList()
    this.getSubjectList()
    this.getUsersList()
  },

  methods: {
    // 获取学科列表
    async getSubjectList () {
      const res = await subjectList()
      this.subjects = res.data.items
    },
    // 获取录入人列表
    async getUsersList () {
      const res = await usersList()
      this.users = res.data
    },
    // 获取目录和标签列表
    async getCatalogAndTagsList () {
      const res = await simple({ subjectID: this.searchForm.subjectID })
      this.catalogs = res.data
      const res1 = await tagsList({ subjectID: this.searchForm.subjectID })
      this.tagsList = res1.data
    },
    // 获取企业列表
    async getCompanyList () {
      const res = await companysList()
      this.companys = res.data.items
    },
    // 获取城市列表
    choseProvince (val) {
      this.cityList = citys(val)
    },
    // 清空表单
    clearFrom () {
      for (const key in this.searchForm) {
        this.searchForm[key] = ''
        this.getQuestionsList()
      }
    },
    // 搜索题目
    async  searchQuestion () {
      const search = {
        ...this.searchForm,
        page: this.page,
        pagesize: this.pagesize
      }
      await this.handleQuestionsList(search)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getQuestionsList()
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.getQuestionsList()
    },
    // 获取试题列表
    async getQuestionsList () {
      await this.handleQuestionsList({ page: this.page, pagesize: this.pagesize })
    },
    // 封装题目列表
    async handleQuestionsList (data) {
      const res = await list(data)
      this.questionsList = res.data.items
      this.total = res.data.counts
      this.questionsList.forEach(item => {
        item.addDate = parseTime(item.addDate)
      })
      this.questionsList.forEach(item => {
        item.question = html2Text(item.question)
      })
    },
    // 删除
    delQuesqion (data) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log(data)
          const res = await remove(data)
          if (res.data.success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: '删除失败!'
            })
          }
          this.getQuestionsList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 加入精选
    async addChoiceness (row) {
      const data = {
        id: row.id,
        choiceState: 1
      }
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await choiceAdd(data)
          if (res.data.success) {
            this.$message({
              type: 'success',
              message: '加入精选成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: '加入精选失败!'
            })
          }
          this.getQuestionsList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消加入精选'
          })
        })
    },
    // 题目预览
    async handleDetail (data) {
      const res = await detail({ id: data.id })
      console.log(res.data)
      this.questionsDetail = res.data
      this.questionsDetail.question = html2Text(this.questionsDetail.question)
      this.questionsDetail.answer = html2Text(this.questionsDetail.answer)
      this.dialogVisible = true
    },
    // 子传父关闭弹窗
    changedialogVisible (val) {
      this.dialogVisible = val
    },
    // 跳转
    jumpAddOrEdit (type, data) {
      if (type === 'add') {
        this.$router.push({ name: 'questions-new' })
      } else {
        this.$router.push({ name: 'questions-new', query: { id: data.id } })
      }
    }
  }
}
</script>

<style scoped lang="scss">
.btn_wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  span {
    font-size: 12px;
    color: pink;
  }
}
.el-pagination__jump {
  margin-left: 0;
}
::v-deep .el-alert__icon.is-big {
  font-size: 16px;
  width: 16px;
}
::v-deep .el-alert__description {
  margin-top: 0;
}
.el-alert-title {
  font-size: 13px;
  line-height: 18px;
}
</style>
